home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / python2.5 / hotshot / stats.py < prev    next >
Text File  |  2008-10-05  |  3KB  |  97 lines

  1. """Statistics analyzer for HotShot."""
  2.  
  3. try:
  4.     import profile
  5.     import pstats
  6. except ImportError, e:
  7.     raise ImportError, str(e) + '; please install the python-profiler package'
  8.  
  9. import hotshot.log
  10.  
  11. from hotshot.log import ENTER, EXIT
  12.  
  13.  
  14. def load(filename):
  15.     return StatsLoader(filename).load()
  16.  
  17.  
  18. class StatsLoader:
  19.     def __init__(self, logfn):
  20.         self._logfn = logfn
  21.         self._code = {}
  22.         self._stack = []
  23.         self.pop_frame = self._stack.pop
  24.  
  25.     def load(self):
  26.         # The timer selected by the profiler should never be used, so make
  27.         # sure it doesn't work:
  28.         p = Profile()
  29.         p.get_time = _brokentimer
  30.         log = hotshot.log.LogReader(self._logfn)
  31.         taccum = 0
  32.         for event in log:
  33.             what, (filename, lineno, funcname), tdelta = event
  34.             if tdelta > 0:
  35.                 taccum += tdelta
  36.  
  37.             # We multiply taccum to convert from the microseconds we
  38.             # have to the seconds that the profile/pstats module work
  39.             # with; this allows the numbers to have some basis in
  40.             # reality (ignoring calibration issues for now).
  41.  
  42.             if what == ENTER:
  43.                 frame = self.new_frame(filename, lineno, funcname)
  44.                 p.trace_dispatch_call(frame, taccum * .000001)
  45.                 taccum = 0
  46.  
  47.             elif what == EXIT:
  48.                 frame = self.pop_frame()
  49.                 p.trace_dispatch_return(frame, taccum * .000001)
  50.                 taccum = 0
  51.  
  52.             # no further work for line events
  53.  
  54.         assert not self._stack
  55.         return pstats.Stats(p)
  56.  
  57.     def new_frame(self, *args):
  58.         # args must be filename, firstlineno, funcname
  59.         # our code objects are cached since we don't need to create
  60.         # new ones every time
  61.         try:
  62.             code = self._code[args]
  63.         except KeyError:
  64.             code = FakeCode(*args)
  65.             self._code[args] = code
  66.         # frame objects are create fresh, since the back pointer will
  67.         # vary considerably
  68.         if self._stack:
  69.             back = self._stack[-1]
  70.         else:
  71.             back = None
  72.         frame = FakeFrame(code, back)
  73.         self._stack.append(frame)
  74.         return frame
  75.  
  76.  
  77. class Profile(profile.Profile):
  78.     def simulate_cmd_complete(self):
  79.         pass
  80.  
  81.  
  82. class FakeCode:
  83.     def __init__(self, filename, firstlineno, funcname):
  84.         self.co_filename = filename
  85.         self.co_firstlineno = firstlineno
  86.         self.co_name = self.__name__ = funcname
  87.  
  88.  
  89. class FakeFrame:
  90.     def __init__(self, code, back):
  91.         self.f_back = back
  92.         self.f_code = code
  93.  
  94.  
  95. def _brokentimer():
  96.     raise RuntimeError, "this timer should not be called"
  97.